package com.istudy.servlet;

import com.istudy.entity.*;
import com.istudy.service.imp.CommentServiceImpl;
import com.istudy.service.imp.UploadServiceImpl;
import com.istudy.service.imp.UserServiceImp;
import com.istudy.service.imp.VideoServiceImpl;
import com.istudy.service.interfaces.UploadService;
import com.istudy.service.interfaces.UserService;
import com.istudy.service.interfaces.VideoService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

@WebServlet("/index")
public class VideoServlet extends AbstractServlet {
    private static VideoService videoService = null;
    private static CommentServiceImpl commentServiceImpl = null;
    private static VideoServiceImpl videoServiceImpl = null;
    private static UserService userService=null;
    private  static UploadService uploadService=null;
    private static int type=1;

    @Override
    public Class getServletClass() {
        return VideoServlet.class;
    }

    @Override
    public void init() throws ServletException {
        super.init();
        videoService = new VideoServiceImpl();
        commentServiceImpl = new CommentServiceImpl();
        videoServiceImpl = new VideoServiceImpl();
        uploadService=new UploadServiceImpl();
        userService=new UserServiceImp();
    }

    public String getshowlist(HttpServletRequest request, HttpServletResponse response) {
        List<Video> list = new ArrayList<>();
        int num = 1;
        if (request.getParameter("currentPage") != null) {
            num = Integer.parseInt(request.getParameter("currentPage"));
        }
        if(request.getParameter("type")!=null){

           type = Integer.parseInt(request.getParameter("type"));
        }
        list = videoService.getVideoList(request, num,type);
        request.getSession().setAttribute("VideoList", list);
        return "welcome";
    }
//模糊查询
    public String getlistbyname(HttpServletRequest request, HttpServletResponse response) {
        List<Video> list = new ArrayList<>();
        int num = 1;
        String name=request.getParameter("videoname");
        if (request.getParameter("currentPage") != null) {
            num = Integer.parseInt(request.getParameter("currentPage"));
        }
        list = videoService.getVideoListByName(request,num,name);
        request.getSession().setAttribute("VideoList" ,list);
        return "welcome";
    }
    public void getvideo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if(request.getSession().getAttribute("user")!=null){
            int id = Integer.parseInt(request.getParameter("id"));
            Video video =null;
            User user=(User)request.getSession().getAttribute("user");
            video=videoService.selectVideoById(id);
            user.setUserMoney(user.getUserMoney()-video.getVideoConsume());

            if (user.getUserMoney()>0){
                userService.updateMoney(user);
                request.setAttribute("video", video);
                request.getRequestDispatcher("/dispaly.php?action=displayPage").forward(request, response);
            }else {
                PrintWriter out = response.getWriter();
                out.print("<script type='text/javascript'>alert('积分不足，上传视频可获取积分');" +
                        "window.location.href=document.referrer;</script>");
            }
        }else {
            PrintWriter out = response.getWriter();
            out.print("<script type='text/javascript'>alert('您还未登录，请登录后在操作');" +
                    "window.location.href=document.referrer;</script>");
    }
    }

    public void setVideoList(HttpServletRequest request) {
        List<Video> videos = videoServiceImpl.selectVideoAll(type);
        request.setAttribute("videos", videos);
    }

    /**
     * 新增视频控制器
     *
     * @param request
     * @param response
     * @return
     */
    public void insertVideo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id=Integer.parseInt(request.getParameter("upuserid"));
        if(id==0){
            id=19;
        }
        User user = userService.getUserById(id);
        Video video = new Video();
        String filename = request.getParameter("name");
        String[] nex = filename.split("\\.");
        String name = nex[0];
        video.setVideoName(name);
        video.setVideoAddress("src/" + filename);
        video.setVideoType(request.getParameter("type"));
        video.setVideoPicture("src/" + request.getParameter("picture"));
        video.setVideoUploadUser(user.getUserName());
        video.setVideoDescrtion(request.getParameter("desc"));
        video.setVideoConsume(Integer.parseInt(request.getParameter("consume")));
        video.setVideoUploadTime(request.getParameter("uptime"));
        video.setVideoConsume(Integer.parseInt(request.getParameter("consume")));
        Upload upload=new Upload();
        PrintWriter out = response.getWriter();
        int upid=1;
        upid=Integer.parseInt(request.getParameter("upid"));
        File file=new File("E:\\javaCode\\AIStudy\\web\\static\\audit\\"+filename);
        boolean b = file.renameTo(new File("E:\\javaCode\\AIStudy\\web\\static\\src\\"+filename));
        if (b) {
            uploadService.deleteUploadById(upid);
            user.setUserMoney(user.getUserMoney()+100);
            userService.updateMoney(user);
            videoService.insertVideo(video);
            out.print("<script type='text/javascript'>alert('审核成功，1秒后将跳转至初始界面');" +
                    "setTimeout( window.location.href=document.referrer,1000);</script>");
        } else {
            out.print("<script type='text/javascript'>alert('上传失败，请核对后再操作');" +
                    "window.location.href=document.referrer;</script>");
        }
    }
}

